package pl.nith.battleships.helpers;

import java.awt.Rectangle;

import pl.nith.battleships.managers.Manager;
import pl.nith.battleships.models.Tile;

public class TileHelper
{
	public static final int tileSize = 32;
	
	public static enum TileType
	{
		// Aircraft Carrier
		AC_START, AC_BODY,
		// Battleship
		BS_START, BS_BODY,
		// Destroyer
		DS_START, DS_BODY,
		// Submarine
		SM_START, SM_BODY,
		// Patrol Boat
		PB_START, PB_BODY,
		// Tile states
		EMPTY, HIT, MISS, SUNK
	}
	
	public static Tile getTileFromCoords(int x, int y)
	{
		int row = x / tileSize;
		int col = y / tileSize;
		
		if (row >= 0 && row < BoardHelper.ROWS && col >= 0 && col < BoardHelper.COLS)
			return Manager.getBoard().getTile(row, col, false);
		if (row >= 16 && row < BoardHelper.ROWS + 16 && col >= 0 && col < BoardHelper.COLS)
			return Manager.getBoard().getTile(row - 16, col, true);
		
		return new Tile(-1, -1, false);
	}
	
	public static Rectangle getTileBoundries(int row, int col, boolean isOnHitBoard)
	{
		if (isOnHitBoard)
		{
			return new Rectangle((row + 16) * tileSize, col * tileSize, tileSize, tileSize);
		}
		
		return new Rectangle(row * tileSize, col * tileSize, tileSize, tileSize);
	}
	
	public static Tile getTileFromField(String field, boolean getHitBoard)
	{
		System.out.println("Field: " + field);
		
		int row = -1, col = -1;
		char rowName = field.charAt(0);
		char columnName = field.charAt(1);
		
		for (int i = 0; i < BoardHelper.ROWS; i++)
		{
			if (rowName == BoardHelper.rowNames[i])
				row = i;
			
			if (columnName == BoardHelper.columnNames[i])
				col = i;
		}
		
		return Manager.getBoard().getTile(row, col, getHitBoard);
	}
	
	public static String getFieldFromTile(int row, int col)
	{		
		return String.valueOf(BoardHelper.rowNames[row]).concat(String.valueOf(BoardHelper.columnNames[col]));
	}
}
